#include <iostream>
#include <queue>

#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;

typedef struct TreeNode *Tree;
struct TreeNode {
    int v;
    Tree Left, Right;
};

Tree Insert(Tree T, char label, char *code);
int CoumputeWPL(int f[],int size);

int main()
{
    int N, M, f[65], RightWPL, NowWPL=0;
    Tree Huffman = NULL;
    char c[65];
    scanf("%d\n", &N);

    for (int i = 0; i < N;i++)
    {
        scanf("%c %d ", &c[i], &f[i]);
    }
    RightWPL = CoumputeWPL(f,N);
    scanf("%d\n", &M);
    char label;
    char code[65];
    //int char_i = 0;//if the input rank is not ABCDEFG
    for (int j = 0; j < M;j++)
    {
        NowWPL = 0;
        for (int i = 0; i < N;i++)
        {
            scanf("%c ", &label);
            scanf("%s\n", code);
            //do something construct Huffman codes
            Huffman = Insert(Huffman, label, code);
            NowWPL += f[i] * (Huffman->v-1);
        }

        Huffman = NULL;
        //check the WPL value if equal to prior one
        if(RightWPL == NowWPL)
            printf("Yes\n");
        else
            printf("No\n");
    }
        return 0;
}

/* ------------- Reference: https://blog.csdn.net/VVVLeHr/article/details/87877439 -------- */

int CoumputeWPL(int f[],int size)
{
    int result=0;
    priority_queue<int, vector<int>, less<int>> q;
    for (int i = 0; i < size;i++)
        q.push(f[i]);
    while(q.size()>=2)
    {
        int a = q.top();
        q.pop();
        int b = q.top();
        q.pop();
        q.push(a + b);
        result += (a + b);
    }
    return result;
}

Tree Insert(Tree T, char label, char *code)
{
    int num;
    if(T==NULL)
    {
        T = (Tree)malloc(sizeof(struct TreeNode));
        T->Left = NULL;
        T->Right = NULL;
        T->v = 0;
    }
    if (*code == '0')
    {
        T->Left = Insert(T->Left, label, ++code);
        num = T->Left->v;
    }

    else if(*code == '1')
    {
        T->Right = Insert(T->Right, label, ++code);
        num = T->Right->v;
    }
    else
    {
        T->v = T->v + 1;
        return T;
    }
    T->v = num + 1;
    return T;
}